package org.example.springboot01.controller;

import org.example.springboot01.pojo.Article;
import org.example.springboot01.pojo.PageBean;
import org.example.springboot01.pojo.Result;
import org.example.springboot01.service.Articleservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class Articlecontroller {
    @Autowired
    private Articleservice articleservice;

    //添加文章
    @PostMapping
    public Result addarticle(@RequestBody  @Validated Article article){
        articleservice.addarticle(article);//插！
        return Result.success();
    }

//    对文章列表实现分页查
    @GetMapping
    public Result<PageBean<Article>> findlist(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state){
        PageBean<Article> pageBean = articleservice.list(pageNum,pageSize,categoryId,state);
        return Result.success(pageBean);
    }
    //获取文章详细
    //放在requestparam里
    @GetMapping("/getarticle")
    public Result<Article> getarticle(@RequestParam Integer id){
        Article article = articleservice.getarticle(id);
        return Result.success(article);
    }
    //实现对文章的更新
    @PutMapping
    public Result updatearticle(@RequestBody Article article){
        articleservice.updatearticle(article);
        return Result.success("更新成功");
    }
    //实现对文章的删除
    @DeleteMapping
    public Result deletearticle(@RequestParam Integer id){
        articleservice.deletearticle(id);
        return Result.success("删除成功");
    }


}
